import logging

import pytest

from resource.pom.memorandum import Memorandum
from utils.manager import Images


class Test:
    app = Memorandum

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面-备忘录应用，长按笔记，点击置顶按钮')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.refresh_layout()
        cake = device.get_element_by_text('蛋糕做法')
        device.long_click_element(cake)
        logging.info('预期结果1.长按笔记，可单选或多选，置顶按钮正常')
        device.refresh_layout()
        device.assert_text_exist('置顶')
        device.assert_text_exist('多选')

        logging.info('步骤2. 点击置顶按钮，查看笔记状态')
        topping_x, topping_y = device.center_of_element(device.get_element_by_text('置顶'))
        device.click(topping_x, topping_y - 30)
        logging.info('预期结果2. 笔记被置放到最上方，且有置顶标识')
        device.refresh_layout()
        _, cake_y = device.center_of_element(device.get_element_by_text('蛋糕做法'))
        _, travel_y = device.center_of_element(device.get_element_by_text('旅游心得'))
        assert cake_y < travel_y, '置顶失败'
        cake_item = device.save_snapshot_to_local('cake.jpeg')
        Images.crop_picture(cake_item, *self.app.top_area)
        similarity = Images.is_image_in_image(self.app.top_pic, cake_item)
        assert similarity > 0.9, '没检查到置顶标识'
